<?php

namespace App\Listeners\V1;

use App\Events\V1\UploadFileEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Zhuzhichao\IpLocationZh\Ip;

class UploadAvatarLogListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UploadFileEvent  $event
     * @return void
     */
    public function handle(UploadFileEvent $event)
    {
        // 从事件中获取相关数据
        $user = $event->getUser();
        $agent = $event->getAgent();
        $ip = $event->getIp();
        $addresses = Ip::find($ip);
        $timestamp = $event->getTimestamp();
        $device = $agent->device();
        $browser = $agent->browser();
        $platform = $agent->platform();
        $language = $agent->languages();
        $imagesPath = $event->getImages();

        // 日志记录格式
        $log = array(
            'user' => $user->getAttribute('code'),
            'ip' => $ip,
            'address' => implode(' ', $addresses),
            'device' => $device,
            'browser' => $browser. ' ' .$agent->version($browser),
            'platform' => $platform.' '.$agent->version($platform),
            'language' => implode(' ',$language),
            'images' => $imagesPath,
            'time' => $timestamp,
        );

        // 日志记录
        Log::info('upload_avatar_success_event ',$log);
    }
}
